package com.cinema.entity.user;

import com.cinema.service.TicketService;
import com.cinema.util.InputUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 前台用户，负责售票、退票操作
 */
@Getter
@Setter
@AllArgsConstructor
public class FrontDeskUser extends AbstractUser implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;
    private final Scanner scanner = new Scanner(System.in);
    private final TicketService ticketService = new TicketService();

    // 保留带参构造器，移除重复的无参构造器定义
    public FrontDeskUser(String userId, String username, String password, LocalDateTime createTime, String userType, String phone) {
        super(userId, username, password, createTime, userType, phone);
    }

    @Override
    public void showMenu() {
        while (true) {
            System.out.println("\n===== 前台菜单 =====");
            System.out.println("1. 修改个人密码");
            System.out.println("2. 查看影片列表");
            System.out.println("3. 查看场次信息");
            System.out.println("4. 查看场次座位图");
            System.out.println("5. 售票操作");
            System.out.println("6. 退票操作");
            System.out.println("7. 退出登录");
            System.out.print("请选择操作[1-7]：");

            int choice = InputUtil.getValidInt();
            switch (choice) {
                case 1:
                    handleChangePassword();
                    break;
                case 2:
                    ticketService.listAllMovies();
                    break;
                case 3:
                    showSchedules();
                    break;
                case 4:
                    showSeatMap();
                    break;
                case 5:
                    sellTicket();
                    break;
                case 6:
                    refundTicket();
                    break;
                case 7:
                    System.out.println("✅ 已退出登录");
                    return;
                default:
                    System.out.println("❌ 无效选择，请重新输入");
            }
        }
    }

    // 修改密码
    private void handleChangePassword() {
        System.out.print("请输入旧密码：");
        String oldPwd = scanner.next();
        System.out.print("请输入新密码：");
        String newPwd = scanner.next();

        if (super.changePassword(oldPwd, newPwd)) {
            System.out.println("✅ 密码修改成功");
        } else {
            System.out.println("❌ 旧密码错误，修改失败");
        }
    }

    // 显示场次
    private void showSchedules() {
        System.out.println("1. 当日场次");
        System.out.println("2. 近3日场次");
        System.out.println("3. 近7日场次");
        System.out.print("请选择：");
        int range = InputUtil.getValidInt();
        ticketService.listSchedules(range);
    }

    // 显示座位图
    private void showSeatMap() {
        System.out.print("请输入场次ID：");
        String scheduleId = scanner.next();
        ticketService.showSeatMap(scheduleId);
    }

    // 售票操作
    private void sellTicket() {
        System.out.print("请输入场次ID：");
        String scheduleId = scanner.next();

        System.out.print("请输入座位数：");
        int seatCount = InputUtil.getValidInt();

        List<String> seatIds = new ArrayList<>();
        for (int i = 0; i < seatCount; i++) {
            System.out.print("请输入第" + (i+1) + "个座位（格式：X排X座）：");
            seatIds.add(scanner.next());
        }

        System.out.println("1. 成人票");
        System.out.println("2. 儿童票");
        System.out.print("请选择票类型：");
        int ticketType = InputUtil.getValidInt();

        ticketService.sellTicket(scheduleId, seatIds, ticketType, super.getUsername());
    }

    // 退票操作
    private void refundTicket() {
        System.out.print("请输入票号：");
        String ticketId = scanner.next();
        ticketService.refundTicket(ticketId, super.getUsername());
    }
}
